package com.example.demo.innoket.factory;

import com.example.demo.innoket.db.DataSource;
import com.example.demo.innoket.db.DbFactory;
import com.example.demo.innoket.generate.Generate;
import com.example.demo.innoket.generate.PHPGenerate;
import com.example.demo.innoket.util.ConvertHandler;
import com.example.demo.innoket.util.FileType;
import com.example.demo.innoket.util.GeneratorConfig;
import com.example.demo.innoket.vo.Table;

/**
 * 代码生成器工厂
 * @author zhangLi
 *
 */
public class CodeGenerateFactory {

	private static Generate gen;
	private static DataSource db = null;
	private static Table table = null;
	protected static GeneratorConfig.ModulePath path = null;

	/**
	 *  初始化
	 * @param tableName　表名
	 */
	public static void init(String tableName, GeneratorConfig.ModulePath pathValue) {
		try {
			db = DbFactory.create();
			table = db.getTable(tableName);
			ConvertHandler.tableHandle(table);
			path = pathValue;
			System.out.println("init:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建entity
	 */
	public static void createEntity() {
		try {
			gen = new PHPGenerate(FileType.ENTITY);
			gen.generate(table,path);
			System.out.println("createEntity:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建entity
	 */
	public static void createRequest() {
		try {
			gen = new PHPGenerate(FileType.REQUESTS);
			gen.generate(table,path);
			System.out.println("createEntity:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建service
	 */
	public static void createService() {
		try {
			gen = new PHPGenerate(FileType.SERVICE_ADMIN);
			gen.generate(table,path);
			System.out.println("createService:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建route
	 */
	public static void createRoute() {
		try {
			gen = new PHPGenerate(FileType.ROUTE);
			gen.generate(table,path);
			System.out.println("createService:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建createController.admin
	 */
	public static void createControllerAdmin() {
		try {
			gen = new PHPGenerate(FileType.CONTROLLER_ADMIN);
			gen.generate(table,path);
			System.out.println("createController:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建createController.inner
	 */
	public static void createControllerInner() {
		try {
			gen = new PHPGenerate(FileType.CONTROLLER_INNER);
			gen.generate(table,path);
			System.out.println("createController:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 创建createController.member
	 */
	public static void createControllerMember() {
		try {
			gen = new PHPGenerate(FileType.CONTROLLER_MEMBER);
			gen.generate(table,path);
			System.out.println("createController:OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		//初始化数据库表
		CodeGenerateFactory.init("finance_log", GeneratorConfig.ModulePath.PHP_COMMON_BASE);

	}

}
